package vule.app;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javafx.embed.swing.JFXPanel;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import vule.audio.AudioUtils;
import vule.color.ColorsUtil;
import vule.components.RoundButton;
import vule.coordinates.CoordinatesManager;

public class MainFrame extends JFrame {

	private static final long serialVersionUID = 1729233823098776690L;
	
	private JPanel content = new JPanel(null);

	public MainFrame() throws HeadlessException {
		super();
		this.setLayout(new GridBagLayout());
		this.setExtendedState(MAXIMIZED_BOTH);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		
		this.add(content, new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
		
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowOpened(WindowEvent e) {
				SwingUtilities.invokeLater(new Runnable() {
					
					@Override
					public void run() {
						new JFXPanel();
						initNewGame();
					}
				});

			}
		});
		
	}


	private void initPanel() {
		
		Image cursorImage = new ImageIcon("resources/mouse/big_red.gif").getImage();
		content.setCursor(getToolkit().createCustomCursor(cursorImage, new Point(0, 0), "Big Red"));
		content.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				super.mouseClicked(e);
				if (e.getButton() == MouseEvent.BUTTON1) {
					AudioUtils.play("/audio/error.mp3");
				}
			}
			
		});
		final CoordinatesManager coordinatesManager = new CoordinatesManager(content);
		final RoundButton roundButton = new RoundButton("");
		roundButton.setBackground(ColorsUtil.getNextColor().getColor());
		roundButton.setBounds(coordinatesManager.getRandomPosition(100, 100));
		roundButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				roundButton.setBounds(coordinatesManager.getRandomPosition(100, 100));
				roundButton.setBackground(ColorsUtil.getNextColor().getColor());
				AudioUtils.play("/audio/tada.mp3");
			}
		});
		content.add(roundButton);
		content.repaint();
	}
	
	
	private void initNewGame() {
		Image cursorImage = getToolkit().getImage("resources/mouse/big_red.gif");
		content.setCursor(getToolkit().createCustomCursor(cursorImage, new Point(0, 0), "Big Red"));
		final CoordinatesManager coordinatesManager = new CoordinatesManager(content);
		addWrongButton(content, coordinatesManager);
		addWrongButton(content, coordinatesManager);
		addRightButton(content, coordinatesManager);
		content.repaint();
	}


	private void addRightButton(final JPanel content,
			final CoordinatesManager coordinatesManager) {
		RoundButton roundButton = new RoundButton("");
		roundButton.setBackground(ColorsUtil.getNextAvailableColor().getColor());
		roundButton.setBounds(coordinatesManager.getRandomFreePosition(100, 100));
		roundButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				AudioUtils.play("/audio/tada.mp3");
				content.removeAll();
				coordinatesManager.clearComponents();
				ColorsUtil.clearUsedColors();
				addWrongButton(content, coordinatesManager);
				addWrongButton(content, coordinatesManager);
				addRightButton(content, coordinatesManager);
				content.repaint();
			}
		});
		content.add(roundButton);
		
	}


	private void addWrongButton(JPanel content,
			CoordinatesManager coordinatesManager) {
		RoundButton roundButton = new RoundButton("");
		roundButton.setBackground(ColorsUtil.getNextAvailableColor().getColor());
		roundButton.setBounds(coordinatesManager.getRandomFreePosition(100, 100));
		roundButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				AudioUtils.play("/audio/error.mp3");
			}
		});
		content.add(roundButton);
	}
	
}
